#include "Box.h"
#include "globals.h"
#include "Utility.h"

const DWORD tri_fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;

// A structure for our custom vertex type
struct tri_vertex
{
    FLOAT x, y, z;      // The transformed position for the vertex
    DWORD color;        // The vertex color
};

bool Box::Draw(D3DXVECTOR3* min, D3DXVECTOR3* max) {

/*	tri_vertex data[]= {
     //Cube vertices
      //Front face
   {-1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f, 1.0f,-1.0f,0xFF0000FF},
   { 1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f,-1.0f,-1.0f,0xFF0000FF},
      //Back face
   { 1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f, 1.0f, 1.0f,0xFF0000FF},
   {-1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f,-1.0f, 1.0f,0xFF0000FF},
      //Top face
   {-1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f, 1.0f,0xFFFF0000},
   { 1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f,-1.0f,0xFFFF0000},
      //Bottom face
   { 1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f, 1.0f,0xFFFF0000},
   {-1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f,-1.0f,0xFFFF0000},
      //Left face
   {-1.0f,-1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f,-1.0f,0xFF00FF00},
   {-1.0f, 1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f, 1.0f,0xFF00FF00},
      //Right face
   { 1.0f,-1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f, 1.0f,0xFF00FF00},
   { 1.0f, 1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f,-1.0f,0xFF00FF00},
	};*/
	tri_vertex data[]= {
     //Cube vertices
		{min->x, min->y, min->z, 0.0f}, {min->x, min->y, max->z, 0.0f}, {max->x, min->y, max->z, 0.0f},
		{min->x, min->y, min->z, 0.0f}, {max->x, min->y, max->z, 0.0f}, {max->x, min->y, min->z, 0.0f},
		{min->x, max->y, min->z, 0.0f}, {min->x, max->y, max->z, 0.0f}, {max->x, max->y, max->z, 0.0f},
		{min->x, max->y, min->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, {max->x, max->y, min->z, 0.0f},
		{min->x, min->y, min->z, 0.0f}, {max->x, min->y, min->z, 0.0f}, {min->x, max->y, min->z, 0.0f},
		{max->x, min->y, min->z, 0.0f}, {max->x, max->y, min->z, 0.0f}, {min->x, max->y, min->z, 0.0f},
		{min->x, min->y, max->z, 0.0f}, {max->x, min->y, max->z, 0.0f}, {min->x, max->y, max->z, 0.0f},
		{max->x, min->y, max->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, {min->x, max->y, max->z, 0.0f},
		{min->x, min->y, min->z, 0.0f}, {min->x, max->y, min->z, 0.0f}, {min->x, max->y, max->z, 0.0f},
		{min->x, min->y, min->z, 0.0f}, {min->x, max->y, max->z, 0.0f}, {min->x, min->y, max->z, 0.0f},
		{max->x, min->y, min->z, 0.0f}, {max->x, max->y, min->z, 0.0f}, {max->x, max->y, max->z, 0.0f},
		{max->x, min->y, min->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, {max->x, min->y, max->z, 0.0f},
	 // Lines vertices
		{min->x, min->y, min->z, 0.0f}, {max->x, min->y, min->z, 0.0f}, 
		{min->x, min->y, max->z, 0.0f}, {max->x, min->y, max->z, 0.0f}, 
		{min->x, max->y, min->z, 0.0f}, {max->x, max->y, min->z, 0.0f}, 
		{min->x, max->y, max->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, 
		{min->x, min->y, min->z, 0.0f}, {min->x, min->y, max->z, 0.0f}, 
		{max->x, min->y, min->z, 0.0f}, {max->x, min->y, max->z, 0.0f}, 
		{min->x, max->y, min->z, 0.0f}, {min->x, max->y, max->z, 0.0f}, 
		{max->x, max->y, min->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, 
		{min->x, min->y, min->z, 0.0f}, {min->x, max->y, min->z, 0.0f}, 
		{min->x, min->y, max->z, 0.0f}, {min->x, max->y, max->z, 0.0f}, 
		{max->x, min->y, min->z, 0.0f}, {max->x, max->y, min->z, 0.0f}, 
		{max->x, min->y, max->z, 0.0f}, {max->x, max->y, max->z, 0.0f}, 
	};
	for(int i = 0; i < sizeof(data) / sizeof(tri_vertex); i++) {
		data[i].color = D3DCOLOR_RGBA(0, 0, 128, 128);
	}
	void *vb_vertices;
	LPDIRECT3DVERTEXBUFFER9 g_pVB;        // Buffer to hold vertices
	HRESULT hr;

	hr = g_engine.getVCard()->CreateVertexBuffer( sizeof(data),      //Length
												  D3DUSAGE_WRITEONLY,//Usage
												  tri_fvf,           //FVF
												  D3DPOOL_MANAGED,   //Pool
												  &g_pVB,            //ppVertexBuffer
												  NULL);             //Handle
	if(FAILED(hr))
	{
	  return hr;
	}

	hr=g_pVB->Lock(0, //Offset
					  0, //SizeToLock
					  &vb_vertices, //Vertices
					  0);  //Flags
	if(FAILED(hr))
	{
	  return hr;
	}

	memcpy( vb_vertices, data,sizeof(data));

	g_pVB->Unlock();

	
	g_engine.getVCard()->SetTexture(0, 0);
	g_engine.getVCard()->SetMaterial(&CUtility::CreateMaterial(0.0f, 0.0f, 0.7f));
	g_engine.getVCard()->SetFVF( tri_fvf );
	g_engine.getVCard()->SetStreamSource( 0, g_pVB, 0, sizeof( tri_vertex ) );
	g_engine.getVCard()->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 );
	g_engine.getVCard()->SetMaterial(&CUtility::CreateMaterial(1.0f, 1.0f, 1.0f));
	g_engine.getVCard()->DrawPrimitive( D3DPT_LINELIST, 36, 12 );

}